package osgi.common.services.job;

import org.apache.log4j.Logger;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.listeners.JobListenerSupport;

import osgi.common.services.EServiceState;
import osgi.common.services.LoggerHelper;
import osgi.common.services.job.client.ICommonJob;

public class GlobalJobListener extends JobListenerSupport {
	
	
	/**
	 * Logger for this class
	 */
	protected static final Logger logger = LoggerHelper.getLogger(GlobalJobListener.class);

	
	@Override
	public String getName() {
		
		String returnString = this.getClass().getName();
		
		return returnString;
	}

	@Override
	public void jobToBeExecuted(JobExecutionContext context) {
		logger.debug("jobToBeExecuted(JobExecutionContext) - start");

		super.jobToBeExecuted(context);
		
		
		Job jj = context.getJobInstance();
		if(jj instanceof ICommonJob){
			((ICommonJob) jj).setServiceState(EServiceState.EXECUTING);
		}

		logger.debug("jobToBeExecuted(JobExecutionContext) - end");
	}

	@Override
	public void jobWasExecuted(JobExecutionContext context,	JobExecutionException jobException) {
		
		logger.debug("jobWasExecuted(JobExecutionContext, JobExecutionException) - start");

		
		super.jobWasExecuted(context, jobException);

		
		Job jj = context.getJobInstance();
		if(jj instanceof ICommonJob){
			((ICommonJob) jj).setServiceState(EServiceState.ACTIVE);
		}
		
		logger.debug("jobWasExecuted(JobExecutionContext, JobExecutionException) - end");
	}


	
	
}
